package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.ScenicSpot;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelsQuery;
import cn.wolfcode.luowowo.article.service.IScenicSpotService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravlesService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.service.IUserRedisService;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.cache.vo.UserFavorsVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserFanService;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annoation.UserParam;
import cn.wolfcoe.luowowo.common.exception.LogicException;
import cn.wolfcoe.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RequestMapping("mine")
public class UserController {

    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private ITravlesService travelService;
    @Reference
    private IUserRedisService userRedisService;

    @Reference
    private IScenicSpotService scenicSpotService;

    @Reference
    private ITravelStatisVOService travelStatisVOService;

    @Reference
    private IUserFanService userFanService;
    @Reference
    private IStrategyDetailService strategyDetailService;



    //这个是"我的窝"的路径操作
    @RequestMapping("/home")
    public String home(Model model, @UserParam UserInfo userInfo){
        if(userInfo==null){
            throw  new LogicException("请先登录");
        }
        //获取个人信息
        UserInfo user = userInfoService.get(userInfo.getId());
        model.addAttribute("user",user);

        List<Travel> list=travelService.queryTravelByTop5(userInfo.getId());
        model.addAttribute("travels",list);

        int total=travelService.queryForTotal(userInfo.getId());
        model.addAttribute("total",total);

        //这个是获取点评信息
        List<ScenicSpot> spot=scenicSpotService.queryForTop5(userInfo.getId());
        model.addAttribute("spot",spot);
        //这个是获取点评总数
        int count=scenicSpotService.queryForCount(userInfo.getId());
        model.addAttribute("count",count);

        //通过作者去查询关注
        List<UserInfo> fans=userInfoService.queryByAuthorId(userInfo.getId());
        model.addAttribute("fans",fans);

        //这个是查询关注总数
        int totalFocus=userFanService.getTotalFocusById(userInfo.getId());
        model.addAttribute("totalFocus",totalFocus);
        //这个是查询粉丝总数
        int totalFans=userFanService.getTotalFansById(userInfo.getId());
        model.addAttribute("totalFans",totalFans);
        return "/homepage";
    }


    //这个是"我的游记"的路径操作
    @RequestMapping("/travels")
    public String travels(Model model, @UserParam UserInfo userInfo){
        if(userInfo==null){
            throw  new LogicException("请先登录");
        }
        //获取个人信息
        UserInfo user = userInfoService.get(userInfo.getId());
        model.addAttribute("user",user);

        List<Travel> list=travelService.queryFroList(userInfo.getId());

        model.addAttribute("travels",list);

        int total=travelService.queryForTotal(userInfo.getId());
        model.addAttribute("total",total);

       int countReply= travelService.queryForReplynum(userInfo.getId());
        model.addAttribute("countReply",countReply);

       int countView= travelService.queryForViewnum(userInfo.getId());
        model.addAttribute("countView",countView);

        return "/mytravelnotes";
    }


    //这个是"我的点评"的路径操作
    @RequestMapping("/reviews")
    public String reviews(Model model, @UserParam UserInfo userInfo){
        if(userInfo==null){
            throw  new LogicException("请先登录");
        }
        //获取个人信息
        UserInfo user = userInfoService.get(userInfo.getId());
        model.addAttribute("user",user);
        //这个是获取点评信息
        List<ScenicSpot> list=scenicSpotService.queryForList(userInfo.getId());
        model.addAttribute("spot",list);
        //这个是获取点评总数
        int count=scenicSpotService.queryForCount(userInfo.getId());
        model.addAttribute("count",count);
        return "/review";
    }


    //这个是"我的收藏"的路径操作
    @RequestMapping("/travelcollections")
    public String collections(Model model, @UserParam UserInfo userInfo,Long tid){
        if(userInfo==null){
            throw  new LogicException("请先登录");
        }
        //获取个人信息
        UserInfo user = userInfoService.get(userInfo.getId());
        model.addAttribute("user",user);

        UserFavorsVO vo = userRedisService.getFavorTravel( userInfo.getId() );
        //攻略收藏
        List<StrategyDetail> strategyDetails = strategyDetailService.queryStrategy(vo.getStrategyIds());
        model.addAttribute("strategyDetails",strategyDetails);

        //游记收藏
        List<Travel> travels=travelService.queryTravel(vo.getTravelsIds());
        model.addAttribute("travels",travels);

        return "/travelcollection";
    }


    //这个是"设置"的路径操作
    @RequestMapping("/settings")
    public String settings(Model model, @UserParam UserInfo userInfo,Long tid){
        if(userInfo==null){
            throw  new LogicException("请先登录");
        }
        UserInfo user = userInfoService.get(userInfo.getId());
        model.addAttribute("user",user);
        return "/setting";
    }

    @ResponseBody
    @RequestMapping("/get")
    public String get(Model model, @UserParam UserInfo userInfo) {

        return "/setting";
    }



    @RequestMapping("/update")
    @ResponseBody
    public Object update(UserInfo user,@UserParam UserInfo userInfo) {
        if(userInfo==null){
            throw  new LogicException("请先登录");
        }
        user.setId(userInfo.getId());
        userInfoService.update(user);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/getByUserId")
    @ResponseBody
    public String getByUserId(Model model, @UserParam UserInfo userInfo) {
        //这个是在修改的时候回显信息
        UserInfo user=userInfoService.queryByUserId( userInfo.getId());
        model.addAttribute("user",user);
        return "/setting";
    }

    @RequestMapping("/headImgUrlUpLoad")
    @ResponseBody
    public Object headImgUrlUpLoad(@UserParam UserInfo userInfo,String headImgUrl){
        userInfoService.updateHeadImgUrl(userInfo.getId(),headImgUrl);
        return AjaxResult.SUCCESS;
    }
}
